package com.socodethis.spaceex;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.socodethis.spaceex.math.cPoint;

public class skyBox {
	Sprite sprite;
	Sprite[] layers;
	cPoint[] layerOffsets;
	Vector2[] zones = new Vector2[ 15 ];
	ArrayList <BackgroundHolder> layerBackground = new ArrayList <BackgroundHolder>();
	ArrayList <Sprite> backgroundSprites = new ArrayList <Sprite>();
	
	public skyBox( Sprite sprite ){
		this.sprite = new Sprite( sprite );
		this.layers = null;
		this.layerOffsets = null;
	}
	
	public void addLayer(Sprite sprite,cPoint offsetIncrement, cPoint dimensions){
		layerBackground.add(new BackgroundHolder(sprite,offsetIncrement,dimensions));
	}
	
	
	public ArrayList<Sprite> returnBackgroundSprites(){
		backgroundSprites.clear();
		for(BackgroundHolder layed: layerBackground){
			layed.moveBackground();
			backgroundSprites.add(layed.getSprite1());
			backgroundSprites.add(layed.getSprite2());
		}
		return backgroundSprites;
	}

	public Sprite getBox( cPoint dimensions, cPoint position ){
		//For now I am assuming that the viewport will always be centered and never move
		sprite.setSize( dimensions.getX(), dimensions.getY() );
		sprite.setPosition( -dimensions.getX() / 2, -dimensions.getY() / 2 );
		return sprite;
	}

	private class BackgroundHolder{
		private Sprite sprite1,sprite2;
		private cPoint offsetIncrement,dimensions,currentOffset;
		public BackgroundHolder(Sprite sprite, cPoint offsetIncrement, cPoint dimensions){
			this.sprite1 = new Sprite(sprite);
			this.sprite2 = new Sprite(sprite);
			this.offsetIncrement = new cPoint(offsetIncrement);
			this.dimensions = dimensions;
			sprite1.setSize( dimensions.getX(), dimensions.getY() );
			sprite2.setSize( dimensions.getX(), dimensions.getY() );
			this.currentOffset = new cPoint(0,0);
		}
		public void moveBackground(){
			currentOffset.addPointsNoNewVar(offsetIncrement);
			if(currentOffset.getX() >= 10.0f){
				currentOffset.setPoint(0, 0);
			}
		}
		public Sprite getSprite1(){
			sprite1.setPosition( (-dimensions.getX() / 2) + currentOffset.getX(), (-dimensions.getY() / 2) + currentOffset.getY() );
			return sprite1;
		}
		public Sprite getSprite2(){
			sprite2.setPosition( (-dimensions.getX() / 2) - sprite1.getWidth()  + currentOffset.getX(), (-dimensions.getY() / 2) + currentOffset.getY() );
			return sprite2;
		}
	}
	
}
